package com.lzy.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.dao.OrderMapper;
import com.lzy.crm.dao.criteria.OrderCriteria;
import com.lzy.crm.domain.Order;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.OrderService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.vo.OrderStatVo;
import com.lzy.crm.vo.OrderVo;

@Service("orderService")
public class OrderServiceImpl extends BaseServiceImpl<Order,OrderCriteria> implements OrderService {

	@Resource
	OrderMapper orderMapper;
	
	@Resource
	UserService userService;
	
	@Override
	public BaseMapper<Order, OrderCriteria> getBaseMapper() {
		return orderMapper;
	}

	
	
	/**
	 * 根据订单Id 查询
	 * @param orId
	 * @return
	 */
	public Order getOrderByorId(String orId){
		Example<OrderCriteria> example = new Example<OrderCriteria>(OrderCriteria.class);
		OrderCriteria orderCriteria = example.createCriteria();
		orderCriteria.andOrIdEqualTo(orId);
		return orderMapper.getObjectByExample(example);
	}
	
	
	/**
	 * 查询玩家充值大于 orMoney 金额的笔数
	 * @param openid
	 * @param orMoney
	 * @return
	 */
	public List<Order> getOrderList(String openid,String orMoney){
		Example<OrderCriteria> example = new Example<OrderCriteria>(OrderCriteria.class);
		OrderCriteria orderCriteria = example.createCriteria();
		orderCriteria.andStatusEqualTo("1");
		orderCriteria.andOpenidEqualTo(openid);
		orderCriteria.andOrMoneyGreaterThanOrEqualTo(orMoney);
		return orderMapper.getListByExample(example);
	}
	
	
	
	/**
	 * 
	 * @param gameId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<OrderVo> getOrderInfoList(String gameId,String status,String startTime,String endTime,PageQuery pageQuery){
		List<OrderVo> orderVoList = new ArrayList<OrderVo>(); 
		List<Order> orderList = orderMapper.getOrderInfoList(gameId, status,startTime, endTime, pageQuery);
		// 组装对象
		for (Order order : orderList) {
			OrderVo orderVo = JsonUtil.fromJson(JsonUtil.toJson(order), OrderVo.class);
			User user = userService.getUserByUnionid(order.getUnionid());
			if(user!=null){
				orderVo.setOutUserName(user.getOutUserName());
			}
			orderVoList.add(orderVo);
		}
		return orderVoList;
	}



	@Override
	public OrderStatVo getOrderStatVo(String gameId, String status,
			String startTime, String endTime) {
		return orderMapper.getOrderStatVo(gameId, status,startTime, endTime);
	}
	
}
